package web.Project;

import domain.Project;
import domain.Users;
import org.apache.commons.beanutils.BeanUtils;
import service.User_Service;
import service.impl.User_ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/upProjectServlet")
public class UpProjectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置页面编码
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //获取Map集合
        Map<String,String[]> map = request.getParameterMap();
        //封装Users对象
        Project project = new Project();
        try {
            BeanUtils.populate(project,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        /*调用service修改*/
        User_Service user_service = new User_ServiceImpl();
        boolean req = user_service.upProject(project);

        if (req) request.setAttribute("succes" ,"修改成功");
        else request.setAttribute("delError" ,"修改失败");

        request.getRequestDispatcher("allProjectServlet").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
